package com.example.tangjiangxu.bitmapdemo;

import android.content.Context;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.os.AsyncTask;
import android.util.Log;
import android.widget.ImageView;

/**
 * Created by Administrator on 2017/3/28 0028.
 * 图片加载器
 */
public class ImageLoader {
    private static final String TAG = "ImageLoader";

    /**
     * 加载图片，先从缓存中取，如果没值，再从本地解析，并把解析的图片放到缓存中
     * @param context
     * @param resId
     * @param reqH
     * @param reqW
     */
    public static void loadImage(Context context, ImageView imageView, int resId, int reqH, int reqW) {
        Bitmap bitmap = ImageCache.getInstance().getBitmapFromMemoryCache(resId + "");
        if (bitmap != null) {
            imageView.setImageBitmap(bitmap);
        } else {
            new BitmapWorkerTask(context, imageView).execute(resId, reqH, reqW);
        }
    }

    /**
     * 加载图片的任务
     */
    static class BitmapWorkerTask extends AsyncTask<Integer, Void, Bitmap>{
        private Context context;
        private ImageView imageView;

        public BitmapWorkerTask(Context context, ImageView imageView) {
            this.context = context;
            this.imageView = imageView;
        }

        @Override
        protected Bitmap doInBackground(Integer... params) {
            Bitmap bitmap = loadImageFromLocal(context, params[0], params[1], params[2]);
            ImageCache.getInstance().addBitmapToMemoryCache(params[0] + "", bitmap);
            return bitmap;
        }

        @Override
        protected void onPostExecute(Bitmap bitmap) {
            imageView.setImageBitmap(bitmap);
        }
    }

    /**
     * 从本地获取照片
     * @param context
     * @param resId
     * @param reqH
     * @param reqW
     * @return
     */
    private static Bitmap loadImageFromLocal(Context context, int resId, int reqH, int reqW) {
        BitmapFactory.Options options = new BitmapFactory.Options();
        //inJustDecodeBounds设置为true, 不进行真正获取图片
        options.inJustDecodeBounds = true;
        //进行图片取样，只获取宽高等参数，不进行真正取样
        BitmapFactory.decodeResource(context.getResources(), resId, options);
        //根据option、所需宽高，算出缩放比例
        options.inSampleSize = calculateinSampleSize(options, reqH, reqW);
        //inJustDecodeBounds为false，真正获取图片
        options.inJustDecodeBounds = false;
        return BitmapFactory.decodeResource(context.getResources(), resId, options);

    }

    /**
     * 计算缩放比例
     * @param options
     * @param reqH
     * @param reqW
     * @return
     */
    private static int calculateinSampleSize(BitmapFactory.Options options, int reqH, int reqW) {
        int sample = 1;
        int height = options.outHeight;
        int widht = options.outWidth;
        Log.e(TAG, "calculateinSampleSize: height:" + height + ",width:" + widht);
        if (height > reqH && widht > reqW) {
            double ratioH = height * 1.0 / reqH;
            double ratioW = widht * 1.0 / reqW;
            sample = (int) (ratioW > ratioH ? Math.round(ratioW) : Math.round(ratioH));
        }
        Log.e(TAG, "calculateinSampleSize: sample:" + sample);
        return sample;
    }
}
